
import api from '../../utils/api'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
const app = getApp()
console.log(app)
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageBaseURL: app.imageBaseURL,
    imageResizeParams: app.imageResizeParams,
    list:[],//列表
    is_popup:0,//青年之家是否更新了资源，1已更新，0未更新
    page:1,
    pagesize:10,
    searchval:'',
    searchnull:false,
  },
  //搜索
  onSearch(e){
    this.setData({
      searchval:e.detail,
      list:[],
      page:1,
      pagesize:10
    })
    this.getFind(this.data.searchval)
  },
  onCancel(e){
    this.setData({
      searchval:e.detail,
      list:[],
      page:1,
      pagesize:10
    })
    this.getFind(this.data.searchval)
  },
  //关系弹框
  closeMask(){
    this.setData({
      is_popup:0,
    })
  },
  //活动详情页
  navDetail(e) {
    wx.navigateTo({
      url: '/pages/index_active_detail/index?id=' + e.currentTarget.dataset.id
    })
  },
  //积分商城
  navMyIntegral(){
    wx.navigateTo({
      url: '/pages/my_integral/index'
    })
  },
  // 发现列表
  async getFind(searchval){
    let data={}
    data['flag'] = 'find_activity_list'
    data['page'] = this.data.page
    data['pagesize'] = this.data.pagesize
    data['location[lng]'] = app.globalData.currpoint.longitude
    data['location[lat]'] = app.globalData.currpoint.latitude
    data['citycode'] =app.globalData.firstAreaId
    data['searchval'] = searchval||''
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result && result.forEach((item)=>{
        item.imgurl = api.checkurl(item.imgurl)
      })
      if( result && result.length>0 ){
        let page = this.data.page+1
        let str = 'list['+(this.data.page-1)+']'
        this.setData({
          [str]:result,page
        })
      }
      if(this.data.list.length == 0){
        this.setData({
          searchnull:true
        })
      }
    }else {
      this.setData({
        list:[]
      })
      if(this.data.list.length == 0){
        this.setData({
          searchnull:true
        })
      }
      // Toast(message)
    }
  },
  //弹框
  async getPop(){
    const {recode, result, message} = await api.send({flag:'find_popup'})
    if(recode === 'SUCC') {
      this.setData({
        is_popup:result.is_popup,
      })
    
    }else {
      // Toast(message)
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getFind(this.data.searchval)
    this.getPop()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // wx.hideTabBar()
    // this.getUser()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
     this.getFind(this.data.searchval)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})